package com.xiaofeng.oss.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.xiaofeng.oss.entity.Result;
import com.xiaofeng.oss.service.FileService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: chc
 * @Date: 2024/02/01/21:50
 * @Description: 文件上传控制层
 */

@RestController
public class FileController {

    @Resource
    private FileService fileService;

    @NacosValue(value = "${storage.service.type}",autoRefreshed = true)
    private String storageType;

    @RequestMapping("/testGetAllBuckets")
    public String testGetAllBuckets() throws Exception {
        List<String> allBucket = fileService.getAllBucket();
        return allBucket.get(0);
    }

    /**
     * 获取文件上传路径
     * @param bucketName
     * @param objectName
     * @return
     * @throws Exception
     */
    @RequestMapping("/getUrl")
    public String getUrl(String bucketName, String objectName) throws Exception {
        return fileService.getUrl(bucketName,objectName);
    }

    /**
     * 上传文件
     * @param uploadFile
     * @param bucketName
     * @param objectName
     * @return
     * @throws Exception
     */
    @RequestMapping("/upload")
    public Result uploadFile(MultipartFile uploadFile,String bucketName, String objectName) throws Exception {
        String url = fileService.updateFile(uploadFile, bucketName, objectName);
        return Result.ok(url);
    }


}
